package site.xuqing.autohelperengine.local.overlay;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.WindowManager;

import site.xuqing.autohelperengine.local.bean.StepLocalBean;
import site.xuqing.autohelperengine.local.bean.StepLocalSaveBean;
import site.xuqing.autohelperengine.util.ScreenUtil;

/**
 * @author xuqing
 */
public class FloatViewManager {
    private static FloatViewManager floatViewManager = new FloatViewManager();
    private WindowManager windowManager;
    private CoverView coverView;
    private MessageView messageView;
    private InputView inputView;
    private MenuView menuView;
    private Context[] contexts = new Context[1];

    private FloatViewManager() {
    }

    public static FloatViewManager getInstance() {
        return floatViewManager;
    }

    public void init(Context context) {
        windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        contexts[0] = context;
    }

    public void showCoverView() {
        hideCoverView();
        coverView = new CoverView(contexts[0]);
        int screenWidth = ScreenUtil.getInstance().screenSizeWidth;
        int screenHeight = ScreenUtil.getInstance().screenSizeHeight;
        WindowManager.LayoutParams lp = getParamsCommon();
        lp.width = screenWidth;
        lp.height = screenHeight;
        coverView.initData();
        windowManager.addView(coverView, lp);
    }

    public void hideCoverView() {
        if (coverView != null) {
            try {
                windowManager.removeView(coverView);
                coverView = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showMessageView(String message) {
        hideMessageView();
        messageView = new MessageView(contexts[0]);
        WindowManager.LayoutParams lp = getParamsCommon();
        lp.y = 100;
        lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
        windowManager.addView(messageView, lp);
        messageView.showMessage(message);
    }

    public void hideMessageView() {
        if (messageView != null) {
            try {
                windowManager.removeView(messageView);
                messageView = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showInputView() {
        hideInputView();
        inputView = new InputView(contexts[0]);
        WindowManager.LayoutParams lp = getParamsHaveEditText();
        lp.gravity = Gravity.CENTER;
        windowManager.addView(inputView, lp);
    }

    public void hideInputView() {
        if (inputView != null) {
            try {
                windowManager.removeView(inputView);
                inputView = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void showStartMenuView(StepLocalSaveBean stepLocalSaveBean) {
        hideMenuView();
        menuView = new MenuView(contexts[0]);
        WindowManager.LayoutParams lp = getParamsCommon();
        lp.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
        windowManager.addView(menuView, lp);
        menuView.setStepBeans(stepLocalSaveBean);
    }

    public void showCreateMenuView(StepLocalBean stepLocalBean) {
        hideMenuView();
        menuView = new MenuView(contexts[0]);
        WindowManager.LayoutParams lp = getParamsCommon();
        lp.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
        windowManager.addView(menuView, lp);
        menuView.setStepLocalBean(stepLocalBean);
    }

    public void hideMenuView() {
        if (menuView != null) {
            try {
                windowManager.removeView(menuView);
                menuView = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 可操作，可返回，但是EditText无焦点获取
     */
    public static WindowManager.LayoutParams getParamsCommon(){
        WindowManager.LayoutParams lp=getParams();
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        lp.flags = lp.flags
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        lp.flags = lp.flags
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        return lp;
    }

    /**
     * 可操作，不可返回，EditText有焦点获取
     */
    public static WindowManager.LayoutParams getParamsHaveEditText(){
        WindowManager.LayoutParams lp=getParams();
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        return lp;
    }

    public static WindowManager.LayoutParams getParams() {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        } else {
            lp.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        //悬浮窗的宽高
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.format = PixelFormat.TRANSPARENT;
        return lp;
    }
}
